<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
	<button id="addButton">增加新房间类型</button>
	<c:if test="${empty requestScope.roomTypeList }">
			<h1>没有类型信息</h1>
	</c:if>
	<c:if test="${!empty requestScope.roomTypeList }">
		<table>
			<tr>
				<th>类型名称</th>
				<th>便利设施</th>
				<th>最多人数</th>
				<th>是否靠窗</th>
				<th>价格</th>
				<th>折扣</th>
				<th>添加时间</th>
				<th>备注</th>
				<th>处理</th>
			</tr>
			<c:forEach items="${requestScope.roomTypeList }" var="roomType">
				<tr>
					<td>${ roomType.name}</td>
					<td>${roomType.facility }</td>
					<td>${roomType.num }</td>
					<td>${roomType.window }</td>
					<td>${roomType.price }</td>
					<td>${roomType.discount }</td>
					<td>${roomType.addDate }</td>
					<td>${roomType.zcomment }</td>
					<td>
						<button class="changeButton" onclick="changeButtonFun(${roomType.id})">修改</button>
						<button onclick="deleteButtonFun(${roomType.id})">删除</button>
					</td>
				</tr>
			</c:forEach>
		</table>
	</c:if>
	<div id="add">
		<input type="hidden" id="roomTypeId">
		<label>类型名称:<input type="text" id="name"></label><br>
		<label>便利设施:</label><textarea cols="20" rows="20" id="facility"></textarea><br>
		<label>最多人数:<input type="text" id="num"></label><br>
		<label>是否靠窗:<input type="checkbox" id="window"></label><br>
		<label>价格：<input type="text" id="price"></label><br>
		<label>折扣：<input type="text" id="discount" value="100"></label><br>
		<label>备注：</label><textarea cols="20" rows="20" id="zcomment"></textarea><br>
		<button id="change">修改</button>
		<button id="submit">提交</button>
		<button id="cancel">取消</button>
	</div>
	<style>
		#add{
			display:none;
		}
		#change{
			display:none;
		}
	</style>
	<script type="text/javascript">
		function clearContent(){
			$('#name').val('')
			$('#facility').text('');
			$('#num').val('')
			$("input[id='window']")[0].checked=false
			$('#price').val('')
			$('#discount').val('')
			$('#zcomment').text('');
		}
		$("#addButton").click(function(){
			clearContent()
			var add=document.getElementById('add')
			add.style.display='block'
			var change=document.getElementById('change')
			change.style.display='none'
			var submit=document.getElementById('submit')
			submit.style.display='inline'
		})
		function changeButtonFun(id){
			var add=document.getElementById('add')
			add.style.display='block'
			var change=document.getElementById('change')
			change.style.display='inline'
			var submit=document.getElementById('submit')
			submit.style.display='none'
			$.get("/hotel/roomType/getRoomTypeInfo?id="+id,{},function(res){
				var tn=eval('(' +res + ')').object
				//alert(ad.username)
				$('#roomTypeId').val(tn.id)
				$('#name').val(tn.name)
				$('#facility').text(tn.facility);
				$('#num').val(tn.num)
				windows=$("input[id='window']")[0]
				windows.checked=tn.window
				$('#price').val(tn.price)
				$('#discount').val(tn.discount)
				$('#zcomment').text(tn.zcomment);
			})
		}
		function deleteButtonFun(id){
			if(confirm("您确定要删除该房间类型吗？")){
				$.post("/hotel/roomType/deleteRoomType.do",{"id":id},function(str){
					var res=eval('(' +str + ')')
					//alert(res.message);
					if(res.code==0){
						window.location.href="/hotel/system/mainPage"
					}else{
						alert(res.message);
					}
				});
			}
		}
		$("#submit").click(function(){
			windows=$("input[id='window']")[0].checked
			data={"name":$("#name").val(),"facility":$("#facility").val(),"num":$("#num").val(),
					"window":windows,"price":$("#price").val(),"discount":$("#discount").val(),"zcomment":$("#zcomment").val()
			}
			//alert(JSON.stringify(data))
			$.post("/hotel/roomType/addRoomType.do",data,function(str){
				var res=eval('(' +str + ')')
				//alert(res.message);
				if(res.code==0){
					//alert("成功？")
					window.location.href="/hotel/system/mainPage"
				}else{
					alert(res.message);
				}
			});
			return ;
		})
		$("#change").click(function(){
			windows=$("input[id='window']")[0].checked
			data={"id":$("#roomTypeId").val(),"name":$("#name").val(),"facility":$("#facility").val(),"num":$("#num").val(),
					"window":windows,"price":$("#price").val(),"discount":$("#discount").val(),"zcomment":$("#zcomment").val()
			}
			//alert(JSON.stringify(data))
			$.post("/hotel/roomType/changeRoomTypeInfo.do",data,function(str){
				var res=eval('(' +str + ')')
				//alert(res.message);
				if(res.code==0){
					window.location.href="/hotel/system/mainPage"
				}else{
					alert(res.message);
				}
			});
			return ;
		})
		$("#cancel").click(function(){
			var add=document.getElementById('add')
			add.style.display='none'
			clearContent()
		})
	</script>
</html>